<?php

namespace app\controllers;

use app\components\WebAuthController;
use app\extensions\weixin\WeiXin;
use app\models\Activity;
use app\models\ActivityApply;
use app\models\Favorite;
use app\models\Job;
use app\models\Region;
use app\models\User;
use app\models\UserProfile;
use yii\web\View;

class UserController extends WebAuthController {

    public $layout = 'user';
    protected $typeViews = [
        User::TYPE_STUDENT => 'student',
        User::TYPE_ORG => 'org',
        User::TYPE_COMPANY => 'company',
    ];

    public function actionIndex() {
        $this->view->title = '个人中心';
        $user = $this->user;
        $type = $this->typeViews[$this->user->type];
        return $this->render(compact('user', 'type'));
    }

    public function actionPub() {
        $this->view->title = '发布中心';
        return $this->render();
    }

    public function actionProfile() {
        $user = $this->user;
        $model = UserProfile::getModel($this->user->id);
        $model->setScenario($user->getIsStudent() ? 'student' : 'company');
        if ($model->school_time <= 0) {
            $model->school_time = '';
        } else {
            $model->school_time = date('Y-m-d H:i', $model->school_time);
        }
        if (!$this->isAjax()) {
            $this->layout = 'detail';
            $this->view->title = '修改资料';
            $data = Region::getPickerData();
            $this->view->registerJs("window.region = {$data};", View::POS_BEGIN);
            return $this->render(compact('model', 'user'));
        }

        $res = $this->success();
        if ($user->load($this->post())) {
            $attributes = ['uname', 'phone'];
            $user->setScenario('save');
            if ($user->getIsVerifyDeny()) {
                $user->status = User::STATUS_VERIFY;
                $attributes[] = 'status';
            }
            $res = $user->save(TRUE, $attributes);
        }

        if (!$model->load($this->post())) {
            return $res;
        }

        $fields = ['style_images', 'identity_images', 'user_images', 'product_images'];
        foreach ($fields as $field) {
            $thumbs = $this->getParam("original_{$field}", []);
            $thumbs = $this->upload($thumbs, $field);
            if (!empty($thumbs)) {
                $model->$field = $thumbs;
            }
        }

        return $model->save();
    }

    protected function upload($thumbs, $field) {
        $uploads = $this->getParam($field);
        if (!empty($uploads)) {
            /* @var $wx WeiXin */
            $wx = \Yii::$app->get('weixin');
            foreach ($uploads as $media_id) {
                $file = $wx->getFileForMediaId($media_id);
                if (NULL !== $file) {
                    $thumbs[] = $file;
                }
            }
        }
        return $thumbs;
    }

    public function actionAvatar() {
        $serverId = $this->getParam('mid');

        if (is_string($serverId) && !empty($serverId)) {
            /* @var $wx WeiXin */
            $wx = \Yii::$app->get('weixin');
            $file = $wx->getFileForMediaId($serverId);
            if (NULL !== $file) {
                $this->user->avatar = $file;
                return $this->user->save();
            }
        }
        return $this->error();
    }

    public function actionIdentity() {
        if ($this->user->type > 0) {
            return $this->redirect(['user/']);
        }
        $user = $this->user;
        $user->setScenario('identity');
        if (!$user->load($this->post())) {
            $this->view->title = '完善资料';
            $this->layout = 'detail';
            return $this->render(compact('user'));
        }

        if ($user->status <= 0) {
            $user->status = $user->getIsStudent() ? User::STATUS_ACTIVE : User::STATUS_VERIFY;
        }
        return $user->save();
    }

    public function actionFavorite() {
        $aid = $this->getParam('aid');
        if (!$this->user->getIsStudent()) {
            return $this->error('学生才能收藏！');
        }

        $activity = Activity::findOne($aid);
        if (!$activity) {
            return $this->error('网络异常！');
        }

        $isFavorite = Favorite::find()->where(['aid' => $aid, 'uid' => $this->user->id])->count() > 0;
        if ($isFavorite) {
            return $this->error('您收藏过啦！');
        }

        $activity->favorites = $activity->favorites + 1;
        $activity->save();

        $model = new Favorite();
        $model->uid = $this->user->id;
        $model->aid = $aid;
        return $model->save();
    }

    public function actions() {
        return [
            'publish' => [
                'class' => 'app\actions\UserPublish',
            ],
            'apply-list' => [
                'class' => 'app\actions\UserPublish',
                'title' => '报名者',
                '_view' => 'publish'
            ],
            'activity-apply' => [
                'class' => 'app\actions\UserApplyList',
                'title' => '报名者',
                'layout' => 'detail'
            ],
            'favorites' => [
                'class' => 'app\actions\UserFavorite'
            ],
            'pre-interview' => [
                'class' => 'app\actions\UserPreInterview',
                'title' => '待面试',
            ],
            'pre-attend' => [
                'class' => 'app\actions\UserPreAttend',
                'title' => '待参加',
            ]
        ];
    }
}
